/* MAIN.C file
 * 
 * Copyright (c) 2002-2005 STMicroelectronics
 */

 #include "stm8l15x_rtc.h"
#include "stm8l15x_clk.h"

  
RTC_TimeTypeDef   RTC_TimeStr;
RTC_InitTypeDef   RTC_InitStr;

 void RTC_Periph_Init(void) ;
 void RTC_restart(void);
  

void RTC_Periph_Init(void)
{
  CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
  CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_1);


  /* Configures the RTC wakeup timer_step = RTCCLK/16 = LSE/16 = 488.28125 us */
  RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);
  /* Enable wake up unit Interrupt */
  RTC_ITConfig(RTC_IT_WUT, ENABLE);
  RTC_SetWakeUpCounter(100);
  RTC_WakeUpCmd(ENABLE);
  RTC_InitStr.RTC_AsynchPrediv = 0x7f;
  RTC_InitStr.RTC_SynchPrediv = 0x00ff;
  RTC_Init(&RTC_InitStr);
}

void RTC_restart(void)
{
  RTC_SetWakeUpCounter(100);
  RTC_WakeUpCmd(ENABLE);
}
void rtcWrDisable(void){

	RTC->WPR = 0x00;
}
void  rtcWrEnable(void){

	RTC->WPR = 0xCA;
	RTC->WPR = 0x53;
	//RTC->ISR1 |= RTC_ISR1_INIT;
}
  



main()
{
		 CLK->CKDIVR = CLK_SYSCLKDiv_2; //
	 
	while(CLK->SCSR != CLK_SCSR_RESET_VALUE){ //HSI
		;
	}
	while(! (CLK->ICKCR&CLK_ICKCR_HSIRDY) ){
		;
	}
	 CLK->PCKENR2 |= 0x04;
	 
	 RTC_DeInit();
	 RTC_Periph_Init();
	 RTC_WriteProtectionCmd(DISABLE);
 
 
	 RTC->CR2 = 0;
 RTC_WriteProtectionCmd(ENABLE);
	 
	_asm("halt");
	while (1);
}